import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, SetMetadata } from '@nestjs/common';
import { GuardService } from './guard.service';
import { CreateGuardDto } from './dto/create-guard.dto';
import { UpdateGuardDto } from './dto/update-guard.dto';
import { RolesGuard } from './roles.guard';
import { ReqUrl, Role } from './role.decorator';
import { ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';

@Controller('guard')
@UseGuards(RolesGuard)// 注册守卫兰拦截
@ApiTags('守卫')
export class GuardController {
  constructor(private readonly guardService: GuardService) {}

  @Post()
  @ApiOperation({summary:"测试admin",description:"请求该接口需要amdin权限"})
  create(@Body() createGuardDto: CreateGuardDto) {
    return this.guardService.create(createGuardDto);
  }

  @Get()
  // @SetMetadata('roles', ['admin'])// 守卫进行权限拦截
  @Role('admin')
  @ApiQuery({name:"xxxx",description:"bbb"})
  findAll(@ReqUrl() url) {
    console.log('guard=>url:', url);
    return this.guardService.findAll();
  }

  @Get(':id')
  @ApiParam({name:"id",description:"用户id",required:true})
  findOne(@Param('id') id: string) {
    return this.guardService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateGuardDto: UpdateGuardDto) {
    return this.guardService.update(+id, updateGuardDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.guardService.remove(+id);
  }
}
